package cn.ffcs.uec.staff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.dao.UecPsndocWadocDao;
import cn.ffcs.uec.staff.model.UecPsndocWadoc;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecPsndocWadocDao")
@Transactional
public class UecPsndocWadocDaoImpl extends BaseDaoImpl implements UecPsndocWadocDao {

	@Override
	public PageInfo queryPageInfoByUecPsndocWadoc(UecPsndocWadoc uecPsndocWadoc, int current, int pageSize) {
		if (null != uecPsndocWadoc) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_PSNDOC_WADOC T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			//判断这个对象是外包人员的数据还是合同制的数据
			if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecPsndocWadoc.getNmStaffType())) {
				sb.append(" AND T.NM_STAFF_TYPE = ? ");
				params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			}
			
			if (!StrUtil.isNullOrEmpty(uecPsndocWadoc.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecPsndocWadoc.getPkPsndoc());
			} else if (!StrUtil.isNullOrEmpty(uecPsndocWadoc.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecPsndocWadoc.getStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecPsndocWadoc.class);
		}
		return null;
	}

	@Override
	public PageInfo queryPsndocWadocPageInfoByUecOutStaffInfo(UecOutStaffInfo uecOutStaffInfo, int current,
			int pageSize) {
		String sql = " SELECT * FROM UEC_PSNDOC_WADOC T, UEC_OUT_STAFF_INFO T1  "
				+ " WHERE T.STAFF_ID = T1.OUT_STAFF_ID AND T1.STATUS_CD = ? AND T.STATUS_CD = ?  AND T.NM_STAFF_TYPE = ? ";
		StringBuilder sb = new StringBuilder(sql);
		List<Object> params = new ArrayList<Object>();
		params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
		params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
		params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
		if (null != uecOutStaffInfo) {
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getOutStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecOutStaffInfo.getOutStaffId());
			}
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getPsnName())) {
				sb.append(" AND T1.PSNNAME LIKE ? ");
				params.add("%" + StringEscapeUtils.escapeSql(uecOutStaffInfo.getPsnName()) + "%");
			}
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getPsnCode())) {
				sb.append(" AND T1.PSNCODE = ? ");
				params.add(uecOutStaffInfo.getPsnCode());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecPsndocWadoc.class);
		}
		return null;
	}

	@Override
	public UecPsndocWadoc findObjectByUecPsndocWadoc(UecPsndocWadoc uecPsndocWadoc) {
		StringBuilder sb = new StringBuilder(
				" SELECT * FROM UEC_PSNDOC_WADOC T WHERE T.STATUS_CD = ? AND T.NM_STAFF_TYPE = ? ");
		List<Object> params = new ArrayList<Object>();
		params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
		params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
		if (null != uecPsndocWadoc) {
			if (!StrUtil.isNullOrEmpty(uecPsndocWadoc.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecPsndocWadoc.getStaffId());
			}
			List<UecPsndocWadoc> uecPsndocWadocList = this.jdbcFindList(sb.toString(), params, UecPsndocWadoc.class);
			if (uecPsndocWadocList.size() > 0) {
				return uecPsndocWadocList.get(0);
			}
		}
		return null;
	}

	@Override
	public UecPsndocWadoc findUecPsndocWadocById(UecPsndocWadoc uecPsndocWadoc) {

		StringBuilder sb = new StringBuilder(
				" SELECT * FROM UEC_PSNDOC_WADOC T WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		if (null != uecPsndocWadoc) {
			if (!StrUtil.isNullOrEmpty(uecPsndocWadoc.getWadocId())) {
				sb.append(" AND T.WADOC_ID = ? ");
				params.add(uecPsndocWadoc.getWadocId());
			}
			List<UecPsndocWadoc> uecPsndocWadocList = this.jdbcFindList(sb.toString(), params, UecPsndocWadoc.class);
			if (uecPsndocWadocList.size() > 0) {
				return uecPsndocWadocList.get(0);
			}
		}
		return null;
	
	}

	@Override
	public UecPsndocWadoc findUecPsndocWadocOnApproval(
			UecPsndocWadoc uecPsndocWadoc) {

		StringBuilder sb = new StringBuilder(
				" SELECT * FROM UEC_PSNDOC_WADOC T WHERE T.STATUS_CD = ? AND T.NM_STAFF_TYPE = ? ");
		List<Object> params = new ArrayList<Object>();
		params.add(BaseUnitConstants.ENTT_STATE_PROCESS);
		params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
		if (null != uecPsndocWadoc) {
			if (!StrUtil.isNullOrEmpty(uecPsndocWadoc.getStaffId())) {
				sb.append(" AND T.STAFF_ID = ? ");
				params.add(uecPsndocWadoc.getStaffId());
			}
			List<UecPsndocWadoc> uecPsndocWadocList = this.jdbcFindList(sb.toString(), params, UecPsndocWadoc.class);
			if (uecPsndocWadocList.size() > 0) {
				return uecPsndocWadocList.get(0);
			}
		}
		return null;
	
	}

}
